package com.fhzn.auth.service.impl;

import com.fhzn.auth.entity.*;
import com.fhzn.auth.mapper.*;
import com.fhzn.auth.service.AuthService;
import com.fhzn.auth.service.RoleService;
import com.fhzn.auth.tools.AuthUtil;
import com.fhzn.auth.web.converter.ResourceConverter;
import com.fhzn.auth.web.converter.RoleConverter;
import com.fhzn.auth.web.converter.UserConverter;
import com.fhzn.auth.web.request.AuthQueryRequest;
import com.fhzn.auth.web.vo.AuthQueryResponseVO;
import com.fhzn.auth.web.vo.ResourceVO;
import com.fhzn.auth.web.vo.RoleVO;
import com.fhzn.auth.web.vo.UserVO;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2024/1/2
 */
@Service
@RequiredArgsConstructor
public class AuthServiceImpl implements AuthService {

    private final UserMapper userMapper;
    private final RoleService roleService;
    private final RoleMapper roleMapper;
    private final ResourceMapper resourceMapper;
    private final RoleResourceMapper roleResourceMapper;


    @Override
    public AuthQueryResponseVO query(AuthQueryRequest request) {
        request.checkBaseParams();

        AuthQueryResponseVO data = this.assembleAuthData(request);

        List<ResourceVO> resourceList = data.getResourceList();
        List<RoleVO> roleList = data.getRoleList();

        List<Integer> roleIds = roleList == null ? null : roleList.stream().map(RoleVO::getId).collect(Collectors.toList());


        if (CollectionUtils.isNotEmpty(roleIds)) {
            data.setRoleResourceList(roleResourceMapper.queryByRoleIdList(roleIds));
        } else {
            data.setRoleResourceList(Lists.newArrayList());
        }

        data.setResourceList(resourceList);
        data.setRoleList(roleList);
        return data;
    }

    /**
     * 组装权限数据
     *
     * @param query 请求参数
     * @return 返回参数
     */
    private AuthQueryResponseVO assembleAuthData(AuthQueryRequest query) {
        AuthQueryResponseVO data = new AuthQueryResponseVO();
        UserVO user = UserConverter.fromUser(userMapper.findUserByNickName(query.getHandler()));
        // 岗位代码
        data.setUser(user);

        // isSuperAdmin
        boolean isSuperAdmin = roleService.isSuperAdmin(query.getHandler());
        data.setSuperAdmin(isSuperAdmin);


        // roles
        List<Role> roles = roleMapper.findRolesByNicknameAndAppcode(query.getInvokeAppName());
        data.setRoleList(RoleConverter.convertToVoList(roles));

        // resources
        List<Resource> resources;
        if (CollectionUtils.isNotEmpty(roles)) {
            if (AuthUtil.isAdmin(roles, query.getInvokeAppName())) {
                // 如果是超级管理员或者应用的管理员
                resources = resourceMapper.findResourcesByAppCode(query.getInvokeAppName());
                data.setResourceList(ResourceConverter.convertToVoList(resources));
            }
        }
        return data;
    }
}
